package com.hanbang.taitaigou.ui.activity.aboutme.myOrder;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.model.GetOrderByID;
import com.hanbang.taitaigou.model.GoodListData;
import com.hanbang.taitaigou.ui.activity.base.BaseActivity;
import com.hanbang.taitaigou.ui.activity.gouwuche.AboutMyOrderCommitActivity;
import com.hanbang.taitaigou.ui.activity.taitaigou.ProductDetailsActivity;
import com.hanbang.taitaigou.ui.widget.ListViewForScrollView;
import com.hanbang.taitaigou.ui.widget.NumberSelectView;
import com.hanbang.taitaigou.utils.db.GwcDbUtil;
import com.hanbang.taitaigou.utils.http.BitmapUtil;
import com.hanbang.taitaigou.utils.http.HttpCallBack;
import com.hanbang.taitaigou.utils.http.HttpLocalUtils;
import com.hanbang.taitaigou.utils.http.HttpRequestParams;
import com.hanbang.taitaigou.utils.http.JsonHelper;
import com.hanbang.taitaigou.utils.ui.LogUtils;
import com.hanbang.taitaigou.utils.ui.ToastUtils;
import com.hanbang.taitaigou.utils.ui.UiUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * 21.0	根据订单ID获取订单信息
 * Created by fanxin on 2016/3/26.
 */
public class AboutMyOrderDetailsActivity extends BaseActivity {

    private GetOrderByID getOrderByID = null;
    private int str;

    //收货地址
    private TextView tv_address;
    //姓名
    private TextView tv_name;
    //电话
    private TextView tv_tel;
    //商品明细
    private ListViewForScrollView lvfsv_list;
    //收货时间
    private TextView tv_deadline;
    //总价
    private TextView tv_total;

    //提交订单
    private Button bt_settlement;


    private double bb;

    private List<GoodListData> list;

    private ListViewAdapyer listViewAdapyer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_my_order_details);
        initView();
        setOnClickListener();
        getOrderByID();
    }

    /*
     * 设置点击事件
     */
    private void setOnClickListener() {
        bt_settlement.setOnClickListener(onClickListener);
    }

    @SuppressWarnings("unused")
    private View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                //提交订单
                case R.id.bt_settlement:
                    Intent intent = new Intent();
                    intent.setClass(AboutMyOrderDetailsActivity.this, AboutMyOrderCommitActivity.class);
//                    intent.putExtra("spxinxi", (Parcelable) list);
//                    intent.putA
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    };

    /*
     * 初始化view
     */
    private void initView() {
        // 设置顶部的标题
        setTop(null, 0, getString(R.string.top_order_details), null, 0);
        setBackArrows(findViewById(R.id.top_bar_left_iv));

        Intent intent = getIntent();
        str = intent.getIntExtra("order_id", -1);


        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_tel = (TextView) findViewById(R.id.tv_tel);
        lvfsv_list = (ListViewForScrollView) findViewById(R.id.lvfsv_list);
        tv_deadline = (TextView) findViewById(R.id.tv_deadline);
        tv_total = (TextView) findViewById(R.id.tv_total);
        bt_settlement = (Button) findViewById(R.id.bt_settlement);

        list = new ArrayList<GoodListData>();
        listViewAdapyer = new ListViewAdapyer();
        lvfsv_list.setAdapter(listViewAdapyer);


    }

    /**
     * 21.0	根据订单ID获取订单信息
     */

    private void getOrderByID() {
        HttpRequestParams httpRequestParams = new HttpRequestParams("GetOrderByID");
        httpRequestParams.addUserId(userData);
//        httpRequestParams.addBodyParameter("User_id",String.valueOf(99));
        httpRequestParams.addBodyParameter("order_id", String.valueOf(100));
//        httpRequestParams.addBodyParameter("order_id",String.valueOf(str));
        LogUtils.e("order_id", String.valueOf(str));
        httpManager.post(httpRequestParams, new HttpCallBack<String>(this, "正在加载...") {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    getOrderByID = JsonHelper.parseObject(jsonObject, GetOrderByID.class);
                    JSONArray resultArray = jsonObject.getJSONArray("goods_detail");
//                    JSONArray js = (JSONArray) getOrderByID.getGoods_detail();
                    if (getOrderByID != null) {
                        tv_address.setText("收货地址：" + getOrderByID.getProvince() + getOrderByID.getCity() + getOrderByID.getArea() + getOrderByID.getAddress());
                        tv_name.setText(getOrderByID.getAccept_name());
                        tv_tel.setText(getOrderByID.getTelphone());
                        tv_deadline.setText(getOrderByID.getDeadline());
                        list.addAll(JsonHelper.parseCollection(resultArray, ArrayList.class, GoodListData.class));
                        tv_total.setText("总价：" + ProductDetailsActivity.sswr(bb));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }

    class ListViewAdapyer extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            // 常见的优化ViewHolder
            ViewHolder viewHolder;
            if (null == convertView) {
                viewHolder = new ViewHolder();
                convertView = UiUtils.getInflaterView(AboutMyOrderDetailsActivity.this, R.layout.item_my_order_details);
                viewHolder.iv_photo = (ImageView) convertView.findViewById(R.id.iv_photo);
                viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                viewHolder.number_select_view = (NumberSelectView) convertView.findViewById(R.id.number_select_view);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //获取数据
            imageManager.bind(viewHolder.iv_photo, HttpLocalUtils.getHttpFileUrl(list.get(position).getImg_url()), BitmapUtil.getImageOptionsBuilder().build());
            viewHolder.tv_name.setText(list.get(position).getGname());
            viewHolder.number_select_view.setOnItemClickListener(new NumberSelectView.OnItemClickListener() {
                @Override
                public boolean onAddClick(int num, NumberSelectView view) {
                    if (GwcDbUtil.addGoodAutoNumber(list.get(position).getDbGwcData())) {
                        list.get(position).setNumber(list.get(position).getNumber() + 1);
                        listViewAdapyer.notifyDataSetChanged();
                        ToastUtils.show(AboutMyOrderDetailsActivity.this, "添加成功", Toast.LENGTH_LONG);
                        bb = bb - list.get(position).getSell_price();
                        tv_total.setText("总价：" + ProductDetailsActivity.sswr(bb));
                        return true;
                    }
                    ToastUtils.show(AboutMyOrderDetailsActivity.this, "添加失败", Toast.LENGTH_LONG);
                    return false;
                }

                @Override
                public boolean onSubClick(int num, NumberSelectView view) {
                    if (GwcDbUtil.subGoodAutoNumber(list.get(position).getDbGwcData())) {
                        list.get(position).setNumber(list.get(position).getNumber() + 1);
                        listViewAdapyer.notifyDataSetChanged();
                        ToastUtils.show(AboutMyOrderDetailsActivity.this, "减少成功", Toast.LENGTH_LONG);
                        bb = bb - list.get(position).getSell_price();
                        tv_total.setText("总价：" + ProductDetailsActivity.sswr(bb));
                        return true;
                    }
                    ToastUtils.show(AboutMyOrderDetailsActivity.this, "减少失败", Toast.LENGTH_LONG);
                    return false;
                }
            });


            return convertView;
        }

        class ViewHolder {
            private ImageView iv_photo;
            private TextView tv_name;
            private NumberSelectView number_select_view;
        }
    }

    @Override
    public void setBackArrows(View back) {
        super.setBackArrows(back);
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
    }


    public double xunhuan() {
        double dd = 0.00;
        for (int i = 0; i < list.size(); i++) {
            dd = list.get(i).getNumber() * list.get(i).getSell_price() + dd;
        }
        return dd;
    }

}
